package com.shujia.data

import java.util.Properties

import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}

import scala.io.{BufferedSource, Source}

object MakeCarDataToKafka {
  def main(args: Array[String]): Unit = {
    /**
      * 读取卡口过车数据将数据保存到kafka中
      *
      */


    val source: BufferedSource = Source.fromFile("D:\\data\\cars.json")

    val carsJson: Iterator[String] = source.getLines()


    /**
      * 创建kafka生产者
      *
      */

    val properties = new Properties
    //指定kafka链接参数

    //指定kafka briker列表
    properties.setProperty("bootstrap.servers", "master:9092")

    //指定key value序列化类
    properties.setProperty("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
    properties.setProperty("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")


    val producer = new KafkaProducer[String, String](properties)


    for (car <- carsJson) {

      println(s"正常产生数据：$car")
      val record = new ProducerRecord[String, String]("cars", car)

      producer.send(record)
      producer.flush()

      Thread.sleep(50)
    }


    source.close()
  }
}
